﻿Imports System
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Drawing.Drawing2D
Imports System.Net

Imports Sunts.HyperV.Common
Imports Sunts.HyperV.ServiceControl



Public Class HyperVBackupManager

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            Dim DatabaseCheck As New DatabaseCheck(My.Settings.ConnectionString)
            If Not DatabaseCheck.Exists Then
                MsgBox("The application will close as the database (" & DatabaseCheck.Location & ") could not be found ", CType(vbCritical + vbOKOnly, MsgBoxStyle), "Database Connection Error")
                Me.Close()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        'Server Panel
        Me.lblSchedule.Parent = PictureBox5
        lblSchedule.BackColor = Color.Transparent
        Me.lblCompleted.Parent = PictureBox4
        lblCompleted.BackColor = Color.Transparent
        Me.chkTop.BackColor = Color.White
        Me.chkBottom.BackColor = Color.White

        RefreshData()
        ResizeColumns()
    End Sub

    Private Sub Form1_Resize(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Resize
        PositionAndResize()
    End Sub

    Private Sub dgvJobs_CellMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvJobs.CellMouseDown
        Try
            If Not e.RowIndex = -1 Then
                Me.dgvJobs.Rows(e.RowIndex).Selected = True
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        RefreshData()
    End Sub


#Region "Resize Methods"
    Private Sub PositionAndResize()
        ResizeBackupJobsPanel()
    End Sub

    Private Sub ResizeBackupJobsPanel()

        panJobsHeader.Width = Me.panServer.Width

        PanelVeryTop.Height = CInt((Me.panServer.Height - PanelVeryTop.Top - 30) / 2)
        PanelVeryTop.Width = Me.panServer.Width - (PanelVeryTop.Left * 2)
        PanelTop.Width = Me.PanelVeryTop.Width
        PictureBox5.Width = PanelVeryTop.Width
        chkTop.Left = (PictureBox5.Right - 25)
        PanelTop.Height = PanelVeryTop.Height - PictureBox5.Height

        PanelVeryBottom.Height = PanelVeryTop.Height
        PanelVeryBottom.Top = PanelVeryTop.Bottom + 10
        PanelVeryBottom.Width = PanelVeryTop.Width
        PanelBottom.Width = PanelVeryBottom.Width
        PictureBox4.Width = PanelVeryBottom.Width
        chkBottom.Left = chkTop.Left
        PanelBottom.Height = PanelVeryBottom.Height - PictureBox4.Height

    End Sub
#End Region

#Region "Server Panel Stuff"
    Private Sub chkTop_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkTop.CheckedChanged
        RepositionPanels()
    End Sub
    Private Sub chkBottom_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkBottom.CheckedChanged
        RepositionPanels()
    End Sub
    Sub RepositionPanels()
        If chkTop.Checked = True And chkBottom.Checked = True Then
            Me.PanelTop.Visible = True
            Me.PanelBottom.Visible = True
            Me.PanelVeryBottom.Top = (Me.PanelVeryTop.Bottom + 10)
            PanelVeryTop.Height = CInt((Me.panServer.Height - PanelVeryTop.Top - 30) / 2)
            PanelVeryBottom.Height = PanelVeryTop.Height
            PanelVeryBottom.Top = PanelVeryTop.Bottom + 10

        ElseIf chkTop.Checked = True And chkBottom.Checked = False Then
            Me.PanelTop.Visible = True
            Me.PanelBottom.Visible = False
            Me.PanelVeryBottom.Height = PictureBox4.Height
            PanelVeryBottom.Top = Me.panServer.Bottom - PanelVeryBottom.Height - StatusStrip1.Height - 10
            PanelVeryTop.Height = PanelVeryBottom.Top - PanelVeryTop.Top - 20
            PanelTop.Height = PanelVeryTop.Height - PictureBox5.Height

        ElseIf chkTop.Checked = False And chkBottom.Checked = True Then
            Me.PanelBottom.Visible = True
            Me.PanelTop.Visible = False
            Me.PanelVeryTop.Height = PictureBox5.Height
            Me.PanelVeryBottom.Top = PanelVeryTop.Bottom + 10
            PanelVeryBottom.Height = Me.panServer.Height - PanelVeryBottom.Top - 10
            PanelBottom.Height = PanelVeryBottom.Height - PictureBox4.Height

        ElseIf chkTop.Checked = False And chkBottom.Checked = False Then
            Me.PanelTop.Visible = False
            Me.PanelVeryTop.Height = PictureBox5.Height
            Me.PanelBottom.Visible = False
            Me.PanelVeryBottom.Height = PictureBox4.Height
            Me.PanelVeryBottom.Top = Me.PanelVeryTop.Bottom + 10
        End If
    End Sub
    Sub ResizeColumns()
        Try
            If dgvJobs.Columns.Count > 1 Then
                dgvJobs.Columns("BackupScheduleID").Visible = False
                dgvJobs.Columns("BackupJobID").Visible = False
                dgvJobs.Columns("ActualEnd").Visible = False
                dgvJobs.Columns("CompletionStatus").Visible = False
            End If

            If dgvCompleted.Columns.Count > 1 Then
                dgvCompleted.Columns("BackupJobID").Visible = False
                dgvCompleted.Columns("BackupScheduleID").Visible = False
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
    Sub RefreshData()
        dgvJobs.DataSource = BackupLog.SelectActiveJobs(My.Settings.ConnectionString)
        dgvCompleted.DataSource = BackupLog.SelectCompletedJobs(My.Settings.ConnectionString)
    End Sub
#End Region



#Region "Service Menu Strip"
    Private Sub InstallRemoteServiceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles InstallRemoteServiceToolStripMenuItem.Click
        Using frmInstallRemoteService As New frmInstallRemoteService
            frmInstallRemoteService.ShowDialog()
            frmInstallRemoteService.StartPosition = FormStartPosition.CenterParent
        End Using
    End Sub
    Private Sub StartServiceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartServiceToolStripMenuItem.Click
        Dim StartService As ServiceControl.ReturnValue = ServiceControl.StartService()
        Select Case StartService
            Case 0
                MessageBox.Show("The service was started successfully")
            Case Else
                MessageBox.Show("The service was not started due to the following error:" & vbCrLf & System.Enum.GetName(GetType(ServiceControl.ReturnValue), StartService))
        End Select
        StartService = Nothing
    End Sub
    Private Sub StopServiceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StopServiceToolStripMenuItem.Click
        Dim StopService As ServiceControl.ReturnValue = ServiceControl.StopService()
        Select Case StopService
            Case 0
                MessageBox.Show("The service was stopped successfully")
            Case Else
                MessageBox.Show("The service was not stopped due to the following error:" & vbCrLf & System.Enum.GetName(GetType(ServiceControl.ReturnValue), StopService))
        End Select
        StopService = Nothing
    End Sub
    Private Sub RestartServiceToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestartServiceToolStripMenuItem.Click
        Dim StopService As ServiceControl.ReturnValue = ServiceControl.StopService()
        Select Case StopService
            Case 0
            Case Else
                MessageBox.Show("The service could not be restarted due to the following error:" & vbCrLf & System.Enum.GetName(GetType(ServiceControl.ReturnValue), StopService))
                Exit Sub
        End Select
        StopService = Nothing

        Dim StartService As ServiceControl.ReturnValue = ServiceControl.StartService()
        Select Case StartService
            Case 0
            Case Else
                MessageBox.Show("The service could not be restarted due to the following error:" & vbCrLf & System.Enum.GetName(GetType(ServiceControl.ReturnValue), StartService))
                Exit Sub
        End Select
        StartService = Nothing

    End Sub
#End Region

#Region "Tools Menu"
    Private Sub MailSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MailSetupToolStripMenuItem.Click
        Using frmMailNotificationSetup As New frmMailNotificationSetup
            frmMailNotificationSetup.ShowDialog()
            frmMailNotificationSetup.StartPosition = FormStartPosition.CenterParent
        End Using
    End Sub
    Private Sub MailServerSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MailServerSetupToolStripMenuItem.Click
        Using frmMailServerSetup As New frmMailServer
            frmMailServerSetup.ShowDialog()
            frmMailServerSetup.StartPosition = FormStartPosition.CenterParent
        End Using
    End Sub
    Private Sub HyperVHostServerSetuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HyperVHostServerSetuToolStripMenuItem.Click
        Using frmHostSetup As New frmHostSetup
            frmHostSetup.StartPosition = FormStartPosition.CenterParent
            frmHostSetup.ShowDialog()
        End Using
    End Sub
    Private Sub BackupDeviceSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupDeviceSetupToolStripMenuItem.Click
        Using frmBackupDevice As New frmBackupDevice
            frmBackupDevice.StartPosition = FormStartPosition.CenterParent
            frmBackupDevice.ShowDialog()
        End Using
    End Sub
    Private Sub BackupJobSetupToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackupJobSetupToolStripMenuItem.Click
        Using frmBackupJob As New frmBackupJob
            frmBackupJob.StartPosition = FormStartPosition.CenterParent
            frmBackupJob.ShowDialog()
        End Using
    End Sub
#End Region

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

    End Sub

    Private Sub DatabaseSelectorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DatabaseSelectorToolStripMenuItem.Click
        Using frmDatabaseSelector As New frmDatabaseSelector
            frmDatabaseSelector.ShowDialog()
            RefreshData()
        End Using
    End Sub
End Class

